﻿//
// WorkflowSharp.Dom.ConditionNode.cs
//
// Authors:
// 	Perciun Andrei (perciun.andrei@gmail.com)
//
// Copyright (C) 2008 Perciun Andrei (perciun.andrei@gmail.com)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;

namespace WorkflowSharp.Dom
{
    using Interfaces;
    using Exceptions;

    public class ConditionNode<Condition, Result, Parent> : WorkflowNode<Parent>, IOnable<Condition, Result, Parent>
        where Condition : ICondition<Result>
        where Result : IEquatable<Result>
    {
        private readonly Dictionary<Result, WorkflowNode<ConditionNode<Condition, Result, Parent>>> nodes = 
            new Dictionary<Result, WorkflowNode<ConditionNode<Condition,Result,Parent>>>();

        internal ConditionNode(Parent parent, string id)
            : base(parent, id)
        {
        }

        #region IOnable<Condition,Result,Parent> Members

        public OnNode<ConditionNode<Condition, Result, Parent>> on(Result conditionResult)
        {
            if (nodes.ContainsKey(conditionResult))
            {
                throw new ExpectedResultDuplicationException(this, conditionResult);
            }

            OnNode<ConditionNode<Condition, Result, Parent>> node = new OnNode<ConditionNode<Condition, Result, Parent>>(this, NewId());
            nodes.Add(conditionResult, node);
            return node;
        }

        #endregion
    }
}
